package com.jjy.controller;

import com.alibaba.fastjson.JSONArray;
import com.jjy.pojo.Goods;
import com.jjy.pojo.Order;
import com.jjy.service.GoodsService;
import com.jjy.service.OrderService;
import com.jjy.tools.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class BackendController {

  @Autowired
  GoodsService goodsService;
  @Autowired
  OrderService orderService;

  @RequestMapping("/widgetsPage")
  public String widgetsRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/widgets";
  }

  @RequestMapping("/starterPage")
  public String starterRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/starter";
  }

  @RequestMapping("/calendarPage")
  public String calendarRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/calendar";
  }

  @RequestMapping("/chartjsPage")
  public String chartjsRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/charts/chartjs";
  }
  @RequestMapping("/flotPage")
  public String flotRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/charts/flot";
  }
  @RequestMapping("/inlinePage")
  public String inlineRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/charts/inline";
  }
  @RequestMapping("/generalUIPage")
  public String generalUIRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/UI/generalUI";
  }
  @RequestMapping("/iconsPage")
  public String iconsRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/UI/icons";
  }
  @RequestMapping("/buttonsPage")
  public String buttonsRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/UI/buttons";
  }
  @RequestMapping("/slidersPage")
  public String slidersRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/UI/sliders";
  }
  @RequestMapping("/generalFormPage")
  public String generalFormRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/forms/general";
  }
  @RequestMapping("/advancedFormPage")
  public String advancedFormRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/forms/advanced";
  }
  @RequestMapping("/editorsFormPage")
  public String editorsFormRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/forms/editors";
  }
  @RequestMapping("/simpleTablePage")
  public String simpleTableRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/tables/simple";
  }

  @RequestMapping("/dataTablePage")
  public String dataFormRequest(HttpServletRequest request, Model model) throws Exception {

    //订单页面需要将订单表数据绑定到前端,默认最新的未处理的十个
    List<Goods> allGoods = goodsService.getAllGoods();
    //需要将商品表的所有数据绑定到前端,以便在创建订单时方便选择,免于手写. , 将数据转换为json格式
    JSONArray allGoodsJsonArray = JsonUtil.convertListToJsonArray(allGoods);
    model.addAttribute("allGoods" , allGoodsJsonArray);

    List<Order> allOrder = orderService.getAllOrder();
    JSONArray allOrderJsonArray = JsonUtil.convertListToJsonArray(allOrder);
    model.addAttribute("allOrder" , allOrderJsonArray);
    return "backend/tables/data";
  }
  /*注册请求 ajax*/
  @RequestMapping("/createNewOrderAjax")
  @ResponseBody
  public String registerRequest(HttpServletRequest request,Model model) throws Exception {
    //1.先去验证
    String[] newOrderGoodsNumbers = request.getParameterValues("newOrderGoodsNumber");
    String[] newOrderGoodsWeights = request.getParameterValues("newOrderGoodsWeight");
    String remark = request.getParameter("remark");
    if(newOrderGoodsNumbers != null && newOrderGoodsWeights != null){
      //获取当前订单表的最大订单编号,加1后作为此新订单的订单编号
      Long currentOrderNo = 100001L;
      Long maxOrderNo = orderService.getMaxOrderNo();
      if(maxOrderNo != null){
        currentOrderNo = ++maxOrderNo;
      }
      for(int i = 0;i < newOrderGoodsNumbers.length;i++){
        Long goodsNumber = Long.valueOf(newOrderGoodsNumbers[i]);
        Double goodsWeight = Double.valueOf(newOrderGoodsWeights[i]);
        Goods goods = goodsService.getGoodsByNo(goodsNumber);
        Order order = new Order();
        order.setNumber(currentOrderNo);
        order.setStatus("等待发货");
        order.setRemark(remark);
        order.setGoodsNumber(goodsNumber);
        if(goods != null){
          order.setGoodsPrice(goods.getPrice());
          order.setOrderPrice((goods.getPrice()) * goodsWeight);
        }
        order.setWeight(goodsWeight);
        orderService.createOrder(order);
      }
    }
    //2.验证通过后,dto转换为实体对象
    if (true) {

      return "ok";
    } else {
      return "error";
    }
  }

  @RequestMapping("/mailboxPage")
  public String mailboxRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/mailbox/mailbox";
  }

  @RequestMapping("/readMailPage")
  public String readMailRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/mailbox/readMail";
  }

  @RequestMapping("/composeMailPage")
  public String composeMailRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/mailbox/compose";
  }

  @RequestMapping("/invoicePage")
  public String invoiceRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/examples/invoice";
  }
  @RequestMapping("/profilePage")
  public String profileRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/examples/profile";
  }
  @RequestMapping("/loginExamplePage")
  public String loginExampleRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/examples/login";
  }

  @RequestMapping("/registerExamplePage")
  public String registerExampleRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/examples/register";
  }

  @RequestMapping("/lockscreenPage")
  public String lockscreenRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/examples/lockscreen";
  }

  @RequestMapping("/page404")
  public String page404Request(HttpServletRequest request, Model model) throws Exception {
    return "backend/examples/404";
  }

  @RequestMapping("/page500")
  public String page500Request(HttpServletRequest request, Model model) throws Exception {
    return "backend/examples/500";
  }

  @RequestMapping("/blankPage")
  public String blankRequest(HttpServletRequest request, Model model) throws Exception {
    return "backend/examples/blank";
  }




  public GoodsService getGoodsService() {
    return goodsService;
  }

  public void setGoodsService(GoodsService goodsService) {
    this.goodsService = goodsService;
  }

  public OrderService getOrderService() {
    return orderService;
  }

  public void setOrderService(OrderService orderService) {
    this.orderService = orderService;
  }
}

